package com.sto.stoapp.manager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.GpsStatus.NmeaListener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

/**
 * GPS管理类
 * 
 * @author Administrator
 * 
 */
public class GpsManager {

	private OnGpsChangedListener onGpsChangedListener;

	private LocationManager lm;

	public LocationManager getLm() {
		return lm;
	}

	private Criteria criteria;

	private Location location;

	private String longitude = "", latitude = "";

	private MyLocationListener locationListener;

	static GpsManager mGpsMsg;

	public static GpsManager getInstance(Context c) {
		if (mGpsMsg != null)
			return mGpsMsg;
		return mGpsMsg = new GpsManager(c);
	}

	public static GpsManager getInstance(Context c, String type) {
		if (mGpsMsg != null)
			return mGpsMsg;
		return mGpsMsg = new GpsManager(c, type);
	}

	public GpsManager(Context context) {
		lm = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		if (!lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
			Toast.makeText(context, "GPS已关闭,请手动开启GPS后再试！", Toast.LENGTH_SHORT)
					.show();
			Toast.makeText(context, "请开启GPS！", Toast.LENGTH_SHORT).show();
			if (context instanceof Activity) {
				Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);

				((Activity) context).startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
			}
			return;
		} else {
			Toast.makeText(context, "GPS定位中...", Toast.LENGTH_SHORT).show();
		}

		initGps();
	}

	public void initGps() {
		criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE); // 设置精确度
		criteria.setAltitudeRequired(true); // 设置请求海拔
		criteria.setBearingRequired(true); // 设置请求方位
		criteria.setCostAllowed(true); // 设置允许运营商收费
		criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

		String provider = lm.getBestProvider(criteria, true);
		location = lm.getLastKnownLocation(provider);
		if (onGpsChangedListener != null) {
			onGpsChangedListener.onGpsChangedListener(location);
		}
		locationListener = new MyLocationListener();
		// 监听2秒一次的位置变化
		lm.requestLocationUpdates(provider, 2 * 1000, 0, locationListener);

		lm.addNmeaListener(listener);
	}

	NmeaListener listener = new NmeaListener() {

		@Override
		public void onNmeaReceived(long timestamp, String nmea) {
			// TODO Auto-generated method stub
			// Log.d("onNmeaReceived", nmea);
		}
	};

	public GpsManager(Context context, String type) {
		lm = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		if (!lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
			Toast.makeText(context, "GPS已关闭,请手动开启GPS后再试！", Toast.LENGTH_SHORT)
					.show();
			Toast.makeText(context, "请开启GPS！", Toast.LENGTH_SHORT).show();
			if (context instanceof Activity) {
				Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
				((Activity) context).startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
			}
			return;
		} else {
			// Toast.makeText(context, "GPS定位中...", Toast.LENGTH_SHORT).show();
		}

		criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE); // 设置精确度
		criteria.setAltitudeRequired(true); // 设置请求海拔
		criteria.setBearingRequired(true); // 设置请求方位
		criteria.setCostAllowed(true); // 设置允许运营商收费
		criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

		String provider = lm.getBestProvider(criteria, true);
		location = lm.getLastKnownLocation(provider);
		if (onGpsChangedListener != null) {
			onGpsChangedListener.onGpsChangedListener(location);
		}
		locationListener = new MyLocationListener();
		// 监听2秒一次的位置变化
		lm.requestLocationUpdates(provider, 2 * 1000, 0, locationListener);
	}

	/**
	 * 获得经度
	 * 
	 * @return
	 */
	public String getLongitude() {
		return longitude;
	}

	/**
	 * 获得纬度
	 * 
	 * @return
	 */
	public String getLatitude() {
		return latitude;
	}

	/**
	 * 设置经纬度
	 * 
	 * @param location
	 */
	private void setLatitudeAndLongitude(Location location) {
		if (location == null) {
			String provider = lm.getBestProvider(criteria, true);
			location = lm.getLastKnownLocation(provider);
		} else {
			// return convertToSexagesimal(location.getLatitude());
			if ("".equals(Math.abs(location.getLatitude()) + "")) {
				latitude = String.valueOf(Math.abs(location.getLatitude()));
			}
			if ("".equals(String.valueOf(Math.abs(location.getLongitude())))) {
				longitude = String.valueOf(Math.abs(location.getLongitude()));
			}
		}
	}

	public void closeGPS() {
		if (lm != null && locationListener != null) {
			lm.removeNmeaListener(listener);
			lm.removeUpdates(locationListener);
		}
	}

	public void requestLocationUpdates() {
		if (lm != null && locationListener != null) {
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2 * 1000,
					0, locationListener);
			lm.addNmeaListener(listener);
		}

	}

	// //获取小数部分
	// private double getdPoint(double num) {
	// String decimal = num + "";
	// decimal = decimal.substring(decimal.indexOf("."));
	// BigDecimal b1 = new BigDecimal(decimal);
	// BigDecimal b2 = new BigDecimal(60);
	// double dPoint = b1.multiply(b2).doubleValue();
	// return dPoint;
	// }

	// //将小数转换为度分秒
	// private String convertToSexagesimal(double num) {
	// int degree = (int) Math.floor(Math.abs(num)); //获取整数部分
	// double temp = getdPoint(Math.abs(num));
	// int minute = (int) Math.floor(temp); //获 取整数部分
	// double second = getdPoint(temp);
	// if (num < 0) {
	// return "-" + degree + "°" + minute + "′" + second + "″";
	// }
	// return degree + "°" + minute + "′" + second + "″";
	// }

	class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			setLatitudeAndLongitude(location);
			if (onGpsChangedListener != null) {
				onGpsChangedListener.onGpsChangedListener(location);
			}
		}

		@Override
		public void onProviderDisabled(String provider) {
			if (onGpsChangedListener != null) {
				onGpsChangedListener.onGpsChangedListener(null);
			}
		}

		@Override
		public void onProviderEnabled(String provider) {

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {

		}
	}

	public void setOnGpsChangedListener(
			OnGpsChangedListener onGpsChangedListener) {
		this.onGpsChangedListener = onGpsChangedListener;
	}

	public interface OnGpsChangedListener {

		void onGpsChangedListener(Location location);
	}
}
